# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib.request
import urllib
import re
import pymysql.cursors
from concurrent.futures import ThreadPoolExecutor
import time
import flask
from flask import request
from flask import jsonify
import b

# 创建一个服务，把当前这个python文件当做一个服务
server = flask.Flask(__name__)

# 连接数据库方法
def sql(img,urltext,urlavi,urlimg):
    connect = pymysql.Connect('localhost','root','root','personaldata')
    # 获取数据库游标
    cursor = connect.cursor()
    # SQL 插入语句
    StringSql = "INSERT INTO url3(url,name, urlavi, urlimg)VALUES ('%s', '%s', '%s', '%s')" % (urltext, img, urlavi, urlimg)
    try:
        # 执行SQL
        cursor.execute(StringSql)
        # 提交到数据库执行
        connect.commit()
    except:
        # 如果发生错误则回滚
        connect.rollback()
    # 关闭数据库连接
    connect.close()  

def getData(i):
    url = "http://www.bibizyz8.com/index-%d.html" % (i)
    response = urllib.request.urlopen(url)
    # 获取最初整个页面
    content = response.read().decode('utf-8')
    # 定义正则表达式
    reg_img =re.compile(r'<a.*?href="(/.+?\/.+?\/)".*?>(.*?)</a>')
    # 匹配正则表达式获取数据
    imglist=reg_img.findall(content)
    for img in imglist:
        urltext=urllib.request.urlopen("http://www.bibizyz8.com"+img[0])
        urlavi=urltext.read().decode('utf-8')
        soup = BeautifulSoup(urlavi, 'html.parser')
        p=soup.find_all('input')
        m=soup.find_all('img')
        urlavi=p[2]['value']
        urlimg=m[1]['src']
        sql(img[1],urltext.url,urlavi,urlimg)
        server.logger.info(img[1])
        
# @server.route()可以将普通函数转变为服务  登录接口的路径、请求方式
@server.route('/get', methods=['get'])
def getUrl():
    start=request.args.get('no')
    end=request.args.get('size')
    arr=[]
    for i in range(int(start),int(end)):
        arr.append(i)
    with ThreadPoolExecutor(5) as executor:
        for each in arr:
            executor.submit(getData,each)

@server.route('/b', methods=['get'])
def chaxun():
    return jsonify(b.shuchu())

server.run(port=5555,debug=True,host='0.0.0.0')     

# if __name__ == '__main__':
#     start=flask.request.values.get('start')
#     end=flask.request.values.get('end')
#     getUrl(start,end)
#服务注册成功
   